package edu.sjtu.erplab.processmining.mxml;

import java.io.File;
import java.io.FileOutputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;

import org.dom4j.Document;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
import org.dom4j.io.OutputFormat;
import org.dom4j.io.XMLWriter;

import edu.sjtu.erplab.processmining.connection.ReadSQL;

public class SQLCreateMXML { 
	
	private static ArrayList<ArrayList<String>> log;
	
	 public void generateDocument() throws SQLException {
		 Document document = DocumentHelper.createDocument();
		 Element mxmlElement = document.addElement("WorkflowLog");
		 //mxmlElement.addComment("An Test for MXML");
		 mxmlElement.addNamespace("xsi","http://www.w3.org/2001/XMLSchema-instance");
		 mxmlElement.addAttribute("xsi:noNamespaceSchemaLocation", "WorkflowLog.xsd");
		 mxmlElement.addAttribute("description", "An Test for MXML");
		 
		 Element sourceElement = mxmlElement.addElement("Source");
		 sourceElement.addAttribute("program", "SQLtoMXML");
		 Element dataElement = sourceElement.addElement("Data");
		 Element attributeElement = dataElement.addElement("Attribute");
		 attributeElement.addAttribute("name", "version");
		 attributeElement.setText("1.0");
		 
		 Element processElement = mxmlElement.addElement("Process");
		 processElement.addAttribute("id", "1");
		 processElement.addAttribute("description", "");		 
		 
		 String name = "";		 
		 int num = 0;		 
		 while((!name.equals(log.get(num).get(1)))&&(num < log.size())){
			 Element processInstanceElement = processElement.addElement("ProcessInstance");			 
			 name = log.get(num).get(1);
			 System.out.println("添加实例" + name);				
			 processInstanceElement.addAttribute("id", name.replaceAll(" ",""));
			 
			 while((name.equals(log.get(num).get(1)))&&(num < log.size())){
				 Element auditTrailEntryElement = processInstanceElement.addElement("AuditTrailEntry"); 
				 Element workflowModelElementElement = auditTrailEntryElement.addElement("WorkflowModelElement"); 
				 workflowModelElementElement.setText(log.get(num).get(5).replaceAll(" ",""));
				 Element eventTypeElement = auditTrailEntryElement.addElement("EventType"); 
				 eventTypeElement.setText(log.get(num).get(4).replaceAll(" ",""));
				 Element timestampElement = auditTrailEntryElement.addElement("Timestamp"); 
				 timestampElement.setText(log.get(num).get(6).replaceAll(" ",""));				
				 if(num == log.size()-1){
					 break;
				 }
				 num++;
				 System.out.println("现在记录" + num);				 
			 }				 
		 }
		 
		 try {	            
			 System.out.println("开始生成XML文件！");
			 OutputFormat format=OutputFormat.createPrettyPrint();
			 XMLWriter output = new XMLWriter(new FileOutputStream("c:/test.xml"),format);	
	         output.write(document);
	         output.close();
	        } catch (IOException e) {
	            System.out.println(e.getMessage());
	     }
	}			 
	 
	 public static void main(String[] args) {
			SQLCreateMXML dom4j = new SQLCreateMXML();
			ReadSQL newConnect = new ReadSQL();		
			newConnect.login();
			try {						
				newConnect.readColumn("TEST");
				newConnect.read("TEST");
			    log = newConnect.getLog();
				dom4j.generateDocument();
				newConnect.close();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}			
	        System.out.println("done");
	 }	   
}
		 

